package aod.android.com.bernay.http;

import android.os.Handler;
import android.os.Looper;

import java.io.IOException;

import chen.lion.hilib.util.LogUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public abstract class BernayCallback implements Callback {

    private Handler mHandler = new Handler(Looper.getMainLooper());

    @Override
    public void onFailure(Call call, final IOException e) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                onHttpFailure(e);
            }
        });

    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        final String json = response.body().string();
        LogUtils.json(json);
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                onJsonData(json);
            }
        });
    }

    public abstract void onJsonData(String json);

    public abstract void onHttpFailure(IOException e);
}
